procedure dist;
var i,j,m:longint;
begin
for i:=1 to n do begin
  for j:=1 to n do d[i][j]:=a[i][j];
end;
for i:=1 to n do d[i][i]:=0;
for m:=1 to n do begin
  for i:=1 to n do begin
    for j:=1 to n do begin
      d[i][j]:=min(d[i][j],d[i][m]+d[m][j]);
    end;
  end;
end;
end;

procedure ways(i,j:longint);
begin
if m[i][j]=1 then begin
  if i=j then write(i,'-',j) else begin
    ways(i,m[i][j]);
    ways(m[i][j],j);
  end;
end;
end;